/**
 * 用于将Promise的结果以 [错误, 数据] 的形式返回，便于 async/await 错误处理
 * @param promise 需要处理的Promise
 * @returns [错误, 数据]
 */
export type ToResult<T, E = unknown> = [error: undefined, data: T] | [error: E, data: T]

export function to<T = any, E = unknown>(promise: Promise<T>): Promise<ToResult<T, E>> {
  return promise
    .then((data) => [undefined, data] as ToResult<T, E>)
    .catch((err: E) => [err, undefined] as ToResult<T, E>)
}
